package fr.utbm.VI51.lemmings_world_editor;

import java.awt.Color;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JPanel;
import javax.swing.border.Border;
import javax.swing.border.TitledBorder;
import javax.swing.event.EventListenerList;


/**
 * @author Adrien
 *
 * This panel contains 3 buttons to Save/Load/Create a new map
 */
public class ButtonPanel extends JPanel{
	
	/**
	 * 
	 */
	private static final long serialVersionUID = 2754133813126741363L;
	private JButton newMap;
	private JButton saveMap;
	private JButton loadMap;
	private final EventListenerList listeners = new EventListenerList();
	
	/**
	 * Constructor
	 * 
	 * @param sTitle the title of the panel
	 */
	public ButtonPanel(String sTitle){
	
		//Borders
		setLayout(new FlowLayout());
		Border border = BorderFactory.createLineBorder(Color.BLACK);
		setBorder(border);
		setBorder(BorderFactory.createTitledBorder(border, sTitle, TitledBorder.CENTER, TitledBorder.TOP));
		
		this.newMap = new JButton("New Map"); //$NON-NLS-1$
		this.saveMap = new JButton("Save"); //$NON-NLS-1$
		this.loadMap = new JButton("Load"); //$NON-NLS-1$
		
		//Listeners
		this.newMap.addActionListener(
				new ActionListener() {
					@Override
					public void actionPerformed(ActionEvent e) {
						for (ButtonListener listener : ButtonPanel.this.listeners.getListeners(ButtonListener.class))
							listener.newMapPressed();	
					}
				}
		);
		this.saveMap.addActionListener(
				new ActionListener() {
					@Override
					public void actionPerformed(ActionEvent e) {
						for (ButtonListener listener : ButtonPanel.this.listeners.getListeners(ButtonListener.class))
							listener.savePressed();	
					}
				}
		);
		this.loadMap.addActionListener(
				new ActionListener() {
					@Override
					public void actionPerformed(ActionEvent e) {
						for (ButtonListener listener : ButtonPanel.this.listeners.getListeners(ButtonListener.class))
							listener.loadPressed();	
					}
				}
		);
		
		this.add(this.newMap);
		this.add(this.saveMap);
		this.add(this.loadMap);
	}
	
	public void addButtonListener(ButtonListener e) {
		this.listeners.add(ButtonListener.class, e);
	}

}
